<?php
/**
 * create to 2021/6/24 9:47
 */

namespace app\shop\controller\sys_admin;


use app\AdminController;
use think\Db;

/**
 * 商品标签相关
 * Class Index
 * @package app\store\controller
 */

class Goodslabel extends AdminController
{

    //*------------------------------------------------------ */
    //-- 首页
    /*------------------------------------------------------ */
    public function index()
    {
        $this->getList(true);
        return $this->fetch('index');
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false, $is_delete = 0)
    {
        $data = Db::table("shop_label")->select();
        $this->assign("data", $data);
        $this->assign("count", Db::table("shop_label")->count());
        return true;
    }

    /*------------------------------------------------------ */
    //-- 添加标签
    /*------------------------------------------------------ */
    public function ajaxAdd() {
        $list = input('list');
        if (empty($list)) return $this->error('传递参数失败！');
        $list_data = explode(",",$list);
        $save_data = [];
        foreach ($list_data as $v) {
            if ($v!="") {
                $save_data[] = ['label'=>$v];
            } else {
                return $this->error('请先输入标签');
                break;
            }
        }
        $data = Db::table("shop_label")->insertAll($save_data);
        if (!$data) return $this->error();
        return $this->success('修改成功.');
    }

    /*------------------------------------------------------ */
    //-- 编辑标签
    /*------------------------------------------------------ */
    public function ajaxEdit() {
        $label_id = input('id', 0, 'intval');
        $label = input('label');
        if ($label_id < 1||empty($label)) return $this->error('传递参数失败！');
        $data = Db::table("shop_label")->where("id",$label_id)->update(['label'=>$label,'update_time'=>time()]);
        if (!$data) return $this->error();
        return $this->success('修改成功.');
    }

    /*------------------------------------------------------ */
    //-- 删除标签
    /*------------------------------------------------------ */
    public function del()
    {
        $label_id = input('label_id', 0, 'intval');
        if ($label_id < 5) return $this->error('传递参数失败！');
        $data = Db::table("shop_label")->where("id",$label_id)->delete();
        if (!$data) return $this->error();
        return $this->success('删除成功.');
    }
}